package cn.edu.sgu.www.mhxysy.controller.prop;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.entity.prop.FilterCondition;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.prop.FilterConditionService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "主题表过滤条件管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/filter_condition", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class FilterConditionController {

    private final FilterConditionService filterRuleService;

    @Autowired
    public FilterConditionController(FilterConditionService filterRuleService) {
        this.filterRuleService = filterRuleService;
    }

    @ApiOperation("保存主题表过滤条件")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult<Void> save(FilterCondition filterCondition) {
        filterRuleService.save(filterCondition);

        return JsonResult.success(CustomMessages.SAVE_SUCCESS);
    }

    @ApiOperation("通过ID删除主题表过滤条件")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "主题表过滤条件ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        filterRuleService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过道具类型ID查询主题表过滤条件")
    @RequestMapping(value = "/selectConditions", method = RequestMethod.GET)
    public JsonResult<List<FilterCondition>> selectConditions(
            @ApiParam(name = "propId", value = "道具类型ID", required = false)
            @RequestParam(required = false) Integer propId) {
        List<FilterCondition> list = filterRuleService.selectConditions(propId);

        return JsonResult.success(null, list);
    }

}